package com.ruiyian.project.equipment.service.impl;

import com.ruiyian.common.annotation.DataScope;
import com.ruiyian.common.core.domain.entity.Equipment;
import com.ruiyian.common.core.domain.entity.SysDictData;
import com.ruiyian.common.vo.TreeSelect;
import com.ruiyian.project.equipment.domain.EquipmentProperty;
import com.ruiyian.project.equipment.domain.EquipmentType;
import com.ruiyian.project.equipment.mapper.EquipmentMapper;
import com.ruiyian.project.equipment.mapper.EquipmentPropertyMapper;
import com.ruiyian.project.equipment.mapper.EquipmentTypeMapper;
import com.ruiyian.project.equipment.service.EquipmentService;
import com.ruiyian.project.road.domain.Dire;
import com.ruiyian.project.road.domain.Road;
import com.ruiyian.project.road.mapper.DireMapper;
import com.ruiyian.project.road.mapper.RoadMapper;
import com.ruiyian.common.core.domain.entity.SysDept;
import com.ruiyian.project.tunnel.domain.Tunnel;
import com.ruiyian.project.tunnel.mapper.TunnelMapper;
import com.ruiyian.system.mapper.SysDeptMapper;
import com.ruiyian.system.mapper.SysDictDataMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/1/19
 * Desc: 设备实现层
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private TunnelMapper tunnelMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private EquipmentTypeMapper equipmentTypeMapper;
    @Autowired
    private RoadMapper roadMapper;
    @Autowired
    private DireMapper direMapper;
    @Autowired
    private EquipmentPropertyMapper equipmentPropertyMapper;
    /**
     * 查询列表
     * @param equipment
     * @return
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<Equipment> selectEquiList(Equipment equipment) {
        return equipmentMapper.selectEquiList(equipment);
    }
    /**
     * 保存
     * @param equipment
     */
    @Override
    public void save(Equipment equipment) {
        equipmentMapper.save(equipment);
    }

    /**
     * 批量保存
     * @param list
     */
    @Transactional
    @Override
    public void batchSave(List<Equipment> list) {
        equipmentMapper.batchSave(list);
    }

    /**
     * 编辑
     * @param equipment
     */
    @Override
    public void edit(Equipment equipment) {
        equipmentMapper.edit(equipment);
    }
    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        equipmentMapper.delete(id);
    }

    /**
     * 根据id获取对象
     * @param id
     * @return
     */
    @Override
    public Equipment findOne(Long id) {
        return equipmentMapper.findOne(id);
    }

    /**
     * 批量删除
     * @param list
     */
    @Override
    public void deleteBatch(List<Equipment> list) {
        equipmentMapper.deleteBatch(list);
    }
    /**
     * 导入
     * @param list
     */
    @Override
    public void importData(List<Equipment> list) {
        list =list.stream().map(p->{
            // 隧道
            if(StringUtils.isNotBlank(p.getTunnelName())){
                List<Tunnel> tunnels = tunnelMapper.selectByName(p.getTunnelName());
                if(tunnels.size()>0){
                    p.setTunnelId(tunnels.get(0).getId());
                }
            }
            // 机构
            if(StringUtils.isNotBlank(p.getDeptName())){
                SysDept sysDept = sysDeptMapper.findByName(p.getDeptName());
                if(null!=sysDept){
                    p.setDeptId(sysDept.getDeptId());
                }
            }
            // 路段
            if(StringUtils.isNotBlank(p.getRoadName())){
                Road road = roadMapper.findByName(p.getRoadName());
                if(null!=road){
                    p.setRoadId(road.getId());
                }else{
                    Road dict = new Road();
                    dict.setName(p.getRoadName());
                    dict.setDeptId(p.getDeptId());
                    roadMapper.save(dict);
                    p.setRoadId(dict.getId());
                }
            }
            // 道路方向
            if(StringUtils.isNotBlank(p.getDirection()) && p.getRoadId()!=null){
                Dire dire = direMapper.findName(p.getRoadId(),p.getDirection());
                if(null!=dire){
                    p.setDireId(dire.getId());
                }else{
                    Dire dict = new Dire();
                    dict.setName(p.getDirection());
                    dict.setRoadId(p.getRoadId());
                    direMapper.save(dict);
                    p.setDireId(dict.getId());
                }
            }

            // 模块
            if(StringUtils.isNotBlank(p.getModuleName())){
                SysDictData sysDictData = new SysDictData();
                sysDictData.setDictLabel(p.getModuleName());
                sysDictData.setParentId(8L);  // 模块
                SysDictData dic = sysDictDataMapper.selectListByName(sysDictData);
                if(null!=dic){
                    p.setModuleId(dic.getDictCode());
                }else{
                    SysDictData dict = new SysDictData();
                    dict.setDictLabel(p.getModuleName());
                    dict.setParentId(8L);
                    dict.setStatus("0");
                    sysDictDataMapper.insertDictData(dict);
                    p.setModuleId(dict.getDictCode());
                }
            }
            // 设备类型
            if(StringUtils.isNotBlank(p.getTypeName())){
                EquipmentType equipmentType = equipmentTypeMapper.selectByName(p.getTypeName());
                if(null!=equipmentType){
                    p.setTypeId(equipmentType.getId());
                    List<EquipmentProperty> equipmentProperties = equipmentPropertyMapper.selectList(new EquipmentProperty(equipmentType.getId()));
                    if(equipmentProperties.size()>0){
                        p.setPropertyId(equipmentProperties.get(0).getId());
                    }
                }else {
                    EquipmentType type = new EquipmentType();
                    type.setName(p.getTypeName());
                    equipmentTypeMapper.save(type);
                    p.setTypeId(type.getId());
                }
            }
            return p;
        }).collect(Collectors.toList());
        this.batchSave(list);
    }


    @Override
    public List<Equipment> selectEquiListByTunnelId(Equipment equipment) {
        return equipmentMapper.selectEquiListByTunnelId(equipment);
    }

    @Override
    public List<Equipment> selectEquiListByType(Equipment equipment) {
        return equipmentMapper.selectEquiListByType(equipment);
    }

    /**
     * 设备按照道路方向
     * @param eq
     * @return
     */
    @Override
    public List<TreeSelect> treeEq(Equipment eq) {
        List<Equipment> equipment = this.selectEquiListByType(eq);
        Map<String, List<Equipment>> collect = equipment.stream().collect(Collectors.groupingBy(Equipment::getRoadName));
        List<TreeSelect> treeSelects = new ArrayList<>();
        for(Map.Entry<String, List<Equipment>> m:collect.entrySet()){
            TreeSelect treeSelect =new TreeSelect();
            treeSelect.setLabel(m.getKey());
            Map<String, List<Equipment>> collect1 = m.getValue().stream().collect(Collectors.groupingBy(Equipment::getDirection));
            List<TreeSelect> treeSelectList = new ArrayList<>();
            for(Map.Entry<String, List<Equipment>> m1:collect1.entrySet()){
                TreeSelect treeSelect1 =new TreeSelect();
                treeSelect1.setLabel(m1.getKey());
                treeSelect1.setChildren(m1.getValue().stream().map(TreeSelect::new).collect(Collectors.toList()));
                treeSelectList.add(treeSelect1);
            }
            treeSelect.setChildren(treeSelectList);
            treeSelects.add(treeSelect);
        }
        return treeSelects;
    }
    /**
     * 设备按照道路
     * @param eq
     * @return
     */
    @Override
    public List<TreeSelect> treeEqNoPoint(Equipment eq) {
        List<Equipment> equipment = this.selectEquiListByType(eq);
        Map<String, List<Equipment>> collect = equipment.stream().collect(Collectors.groupingBy(Equipment::getRoadName));
        List<TreeSelect> treeSelects = new ArrayList<>();
        for(Map.Entry<String, List<Equipment>> m:collect.entrySet()){
            TreeSelect treeSelect =new TreeSelect();
            treeSelect.setLabel(m.getKey());
            treeSelect.setId(0L);
            Map<Integer, List<Equipment>> collect1 = m.getValue().stream().collect(Collectors.groupingBy(Equipment::getIfVirtual));
            List<TreeSelect> treeSelectList = new ArrayList<>();
            for(Map.Entry<Integer, List<Equipment>> m1:collect1.entrySet()){
                TreeSelect treeSelect1 =new TreeSelect();
                treeSelect1.setLabel(ObjectUtils.notEqual(1,m1.getKey())?"外场":"仿真");
                treeSelect1.setId(0L);
                Map<String, List<Equipment>> collect2 = m1.getValue().stream().collect(Collectors.groupingBy(Equipment::getStatusValue));
                List<TreeSelect> treeSelectList1 = new ArrayList<>();
                for(Map.Entry<String, List<Equipment>> m2:collect2.entrySet()){
                    TreeSelect treeSelect2 =new TreeSelect();
                    treeSelect2.setLabel(m2.getKey());
                    treeSelect2.setChildren(m2.getValue().stream().map(TreeSelect::new).collect(Collectors.toList()));
                    treeSelectList1.add(treeSelect2);
                }
                treeSelect1.setChildren(treeSelectList1);
                treeSelectList.add(treeSelect1);
            }
            treeSelect.setChildren(treeSelectList);
            treeSelects.add(treeSelect);
        }
        return treeSelects;
    }

    @Override
    public void updateCode(Equipment equipment) {
        equipmentMapper.updateCode(equipment);
    }

    @Override
    public void updatePoint(Equipment equipment) {
        equipmentMapper.updatePoint(equipment);
    }

    @Override
    public List<Equipment> selectVideoList(Long tunnelId) {
        return equipmentMapper.selectVideoList(tunnelId);
    }
}
